﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Garfield.Presentation.Data
{
    public class ValueConverter<SoruceType, TargetType> : IValueConverter
    {
        private readonly Func<SoruceType, object, TargetType> convertToFunc;
        private readonly Func<TargetType, object, SoruceType> convertBackFunc;

        public ValueConverter(Func<SoruceType, TargetType> convertToFunc, Func<TargetType, SoruceType> convertBackFunc)
            : this((source, parameter) => convertToFunc(source), (target, parameter) => convertBackFunc(target))
        {
        }

        public ValueConverter(Func<SoruceType, TargetType> convertToFunc)
            : this(convertToFunc, null)
        {
        }

        public ValueConverter(Func<SoruceType, object, TargetType> convertToFunc, Func<TargetType, object, SoruceType> convertBackFunc)
        {
            this.convertToFunc = convertToFunc;
            this.convertBackFunc = convertBackFunc;
        }

        public ValueConverter(Func<SoruceType, object, TargetType> convertToFunc)
            : this(convertToFunc, null)
        {
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return convertToFunc((SoruceType)value, parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return convertBackFunc((TargetType)value, parameter);
        }
    }
}
